#!/bin/sh
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/hadoop/bin:/root/bin:/usr/bin:/usr/sbin:/sbin:/bin:.
export LC_ALL=en_US.UTF-8
umask 0022

rootdir=/data1/monitorlog
if ( ($(df -h | awk '{print $NF}' | grep -w "/data1" | wc -l) == 0)); then
  rootdir=/data/monitorlog
  mkdir -p /data1 ${rootdir}
  test -e /data1/monitorlog || ln -s /data/monitorlog /data1/monitorlog
fi

#iotop
ps -ef | grep "iotop -d 2 -o -P -t" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null
mkdir -p ${rootdir}/iotoplog
nohup iotop -d 2 -o -P -t >> ${rootdir}/iotoplog/iotoplog.$(date '+%Y%m%d') 2>&1 &

#top
ps -ef | grep "top -d3 -b" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null
mkdir -p ${rootdir}/toplog
nohup top -d3 -b -c -w512 >> ${rootdir}/toplog/toplog.$(date '+%Y%m%d') 2>&1 &

#dstat
ps -ef | grep "dstat -t -a --proc-count" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null
mkdir -p ${rootdir}/dstatlog
nohup dstat -t -a --proc-count -i -l -m -p  --aio --disk-util  >> ${rootdir}/dstatlog/dstatlog.$(date '+%Y%m%d') 2>&1 &

#iostat
ps -ef | grep "iostat -t -x -m 1" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null
mkdir -p ${rootdir}/iostatlog
export S_TIME_FORMAT=ISO
nohup  iostat -t -x -m 1  >> ${rootdir}/iostatlog/iostatlog.$(date '+%Y%m%d') 2>&1 &

#vmstat
ps -ef | grep "vmstat -t -w 1" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null
test -d ${rootdir}/vmstat || mkdir -p ${rootdir}/vmstat
vmstat -t -w 1 >> ${rootdir}/vmstat/vmstat.log.$(date '+%Y%m%d') 2>&1 &

#meminfo
ps -ef | grep "./runcmd.sh cat /proc/meminfo" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null
test -d ${rootdir}/meminfo || mkdir -p ${rootdir}/meminfo
nohup ./runcmd.sh "cat /proc/meminfo" ${rootdir}/meminfo meminfo.log 2>&1 &
#nohup ./runcmd.sh "cat /proc/vmstat" ${rootdir}/vmstat vmstat.log 2>&1 &

#arp
test -d ${rootdir}/netstat || mkdir -p ${rootdir}/netstat
ps -ef | grep "./runcmd.sh arp" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null
nohup ./runcmd.sh "arp -an" ${rootdir}/netstat arp.log 2>&1 &
#linkstat
ps -ef | grep "./runcmd.sh ip -s link" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null
nohup ./runcmd.sh "ip -s link" ${rootdir}/netstat linkstat.log 2>&1 &
#netstat
ps -ef | grep "./runcmd.sh netstat -s" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null
nohup ./runcmd.sh "netstat -s" ${rootdir}/netstat netstat.log 2>&1 &
#ping
ps -ef | grep "./monitornet.sh" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null
ps -ef | grep "ping -D -W" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null
nohup ./monitornet.sh ${rootdir}/netstat 2>&1 &

#clean history log
find ${rootdir} -type f -mtime +8 | grep monitorlog | xargs ./srmnew 2> /dev/null

#查看监控进程
#ps -ef | grep -E "iotop|top|dstat|iostat|meminfo|vmstat"

ps -ef | grep "/data/monitorcmd/free/free_page_cache.sh" | grep -v grep | awk '{print $2}' | xargs -r kill -9 2> /dev/null
nohup ./free/free_page_cache.sh '/data/monitorcmd/free/free_page_cache.sh' > free/free_page_cache_nohup.log 2>&1 &
